package com.rainkaze.traffic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import com.rainkaze.traffic.R;
import com.rainkaze.traffic.model.Device;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {

    private List<Device> deviceList;
    private Context context;

    public DeviceAdapter(Context context, List<Device> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        Device device = deviceList.get(position);
        holder.deviceName.setText(device.getDeviceName());
        holder.deviceLocation.setText("位置: " + device.getLocation());

        // 根据设备类型设置主图标 (可选)
        if ("Camera".equalsIgnoreCase(device.getDeviceType())) {
            holder.deviceIcon.setImageResource(R.drawable.ic_camera);
        } else {
            holder.deviceIcon.setImageResource(R.drawable.ic_devices);
        }

        // =======================================================
        //                 *** 核心修改逻辑 ***
        //  根据设备状态，设置状态文字、颜色和旁边的连接/断开图标
        // =======================================================
        if ("Online".equalsIgnoreCase(device.getStatus()) || "在线".equals(device.getStatus())) {
            // 设置为在线状态
            holder.deviceStatus.setText("ONLINE");
            holder.deviceStatus.setTextColor(ContextCompat.getColor(context, R.color.status_online));
            holder.statusIcon.setImageResource(R.drawable.ic_wifi_connected);
        } else {
            // 设置为离线状态
            holder.deviceStatus.setText("OFFLINE");
            holder.deviceStatus.setTextColor(ContextCompat.getColor(context, R.color.status_danger)); // 使用红色
            holder.statusIcon.setImageResource(R.drawable.ic_wifi_disconnected);
        }
    }

    @Override
    public int getItemCount() {
        return deviceList == null ? 0 : deviceList.size();
    }

    public void setDevices(List<Device> devices) {
        this.deviceList = devices;
        notifyDataSetChanged();
    }

    // 更新 ViewHolder 以包含新的 imageViewStatusIcon
    static class DeviceViewHolder extends RecyclerView.ViewHolder {
        ImageView deviceIcon, statusIcon; // statusIcon 是新增的
        TextView deviceName, deviceLocation, deviceStatus;

        public DeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            deviceIcon = itemView.findViewById(R.id.imageViewDeviceIcon);
            deviceName = itemView.findViewById(R.id.textViewDeviceName);
            deviceLocation = itemView.findViewById(R.id.textViewDeviceLocation);
            deviceStatus = itemView.findViewById(R.id.textViewDeviceStatus);
            statusIcon = itemView.findViewById(R.id.imageViewStatusIcon); // 关联新的状态图标
        }
    }
}